package com.party.service.excellentparty.impl;

import com.party.common.Page;
import com.party.mapper.ExcellentPartyMapper;
import com.party.model.ExcellentParty;
import com.party.model.ExcellentPartyExample;
import com.party.model.SysUser;
import com.party.service.excellentparty.ExcellentPartyService;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class ExcellentPartyServiceImpl implements ExcellentPartyService {

    @Resource
    ExcellentPartyMapper excellentPartyMapper;

    @Resource
    SysUserService sysUserService;

    @Override
    public int addExcellentParty(ExcellentParty excellentParty) {
        int num = excellentPartyMapper.insertSelective(excellentParty);
        return num;
    }

    @Override
    public int deleteExcellentPartyByPrimaryKey(Integer id) {
        int num = excellentPartyMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExcellentPartyByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += excellentPartyMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public ExcellentParty getExcellentPartyByPrimaryKey(Integer id) {
        ExcellentParty excellentParty = excellentPartyMapper.selectByPrimaryKey(id);
        if(excellentParty!=null){
            SysUser sysUser=sysUserService.getUser(excellentParty.getUid());
            if(sysUser!=null){
                excellentParty.setHeadurl(sysUser.getHeadurl());
            }
        }

        return excellentParty;
    }

    @Override
    public int updateExcellentPartyByPrimaryKey(ExcellentParty excellentParty) {
        int num = excellentPartyMapper.updateByPrimaryKeySelective(excellentParty);
        return num;
    }

    @Override
    public Page getExcellentPartyList(ExcellentParty excellentParty, int pageSize, int pageNumber) {
        ExcellentPartyExample excellentPartyExample = new ExcellentPartyExample();
        /**
         * 可以添加查询条件
         */
        if(excellentParty != null)
        {
            ExcellentPartyExample.Criteria criteria = excellentPartyExample.createCriteria();
            if(excellentParty.getTitle() != null && !"".equals(excellentParty.getTitle()))
            {
                criteria.andTitleLike("%"+excellentParty.getTitle()+"%");
            }

            if(excellentParty.getAuthor() != null && !"".equals(excellentParty.getAuthor()))
            {
                criteria.andAuthorLike("%" + excellentParty.getAuthor() + "%");
            }

            if(excellentParty.getType() != null && !"".equals(excellentParty.getType()))
            {
                criteria.andTypeEqualTo(excellentParty.getType());
            }

        }
        excellentPartyExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = excellentPartyMapper.countByExample(excellentPartyExample);
        //获取指定的页码数据
        excellentPartyExample.setBeginNum((pageNumber - 1) * pageSize);
        excellentPartyExample.setEndNum(pageSize);
        List<ExcellentParty> list = excellentPartyMapper.getListPage(excellentPartyExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }

    @Override
    public int addExcellentPartyByUserIds(String[] userids) {
        int num=0;
        if(!"".equals(userids)&&userids!=null){
            excellentPartyMapper.deleteByExample(new ExcellentPartyExample());
            for (String uid : userids) {
                ExcellentParty excellentParty=new ExcellentParty();
                Integer uidInt=Integer.parseInt(uid);
                excellentParty.setUid(uidInt);
                ExcellentPartyExample excellentPartyExample=new ExcellentPartyExample();
                ExcellentPartyExample.Criteria criteria=excellentPartyExample.createCriteria();
                criteria.andUidEqualTo(uidInt);
                List<ExcellentParty> list=excellentPartyMapper.selectByExample(excellentPartyExample);

                if(list.size()==0){
                    num += excellentPartyMapper.insertSelective(excellentParty);
                }
                num +=1;
            }
        }
        return num;
    }

    @Override
    public List<ExcellentParty> getAllExcellentParty() {
        ExcellentPartyExample excellentPartyExample = new ExcellentPartyExample();
        List<ExcellentParty> list=this.excellentPartyMapper.getAllExcellentParty(excellentPartyExample);
        return list;
    }

    @Override
    public List<ExcellentParty> getAllExcellentPartyByCustom() {
        List<ExcellentParty> list = excellentPartyMapper.getAllExcellentPartyByCustom();
        return list;
    }

    public Page getListPageByCustorm(ExcellentParty excellentParty, int pageSize, int pageNumber){
        ExcellentPartyExample excellentPartyExample = new ExcellentPartyExample();

        //获取总的记录数量
        int count = excellentPartyMapper.countByExample(excellentPartyExample);
        //获取指定的页码数据
        excellentParty.setBeginNum((pageNumber - 1) * pageSize);
        excellentParty.setEndNum(pageSize);
        List<ExcellentParty> list = excellentPartyMapper.getListPageByCustorm(excellentParty);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
